package qqclient.service;

import qqcommon.Message;
import qqcommon.MessageType;

import java.io.*;

public class FileClientService {

    public void sendFileToOne(String src, String dest, String senderID, String getterID) {
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_FILE_MES);
        message.setSender(senderID);
        message.setGetter(getterID);
        message.setSrc(src);
        message.setDest(dest);

        FileInputStream fileInputStream = null;
        byte[] fileBytes = new byte[(int)new File(src).length()];

        try{
            fileInputStream = new FileInputStream(src);
            fileInputStream.read(fileBytes);
            message.setFileBytes(fileBytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //发送
        try {
            ObjectOutputStream oos = new ObjectOutputStream(ManageClientConnectServerThread.getClientConnectServerThread(senderID).getSocket().getOutputStream());
            oos.writeObject(message);
            System.out.println(message.getMesType());
            System.out.println("\n"+senderID+" 给 "+getterID+" 发送了文件："+src+"到对方目录:"+dest);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
